package com.allwees.core.common.util.csv;

import com.opencsv.bean.AbstractBeanField;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/8/18 9:58
 */
public class StringToBigDecimal extends AbstractBeanField<Object,Object> {

    private static final Pattern DECIMAL = Pattern.compile("((([1-9]\\d*)|([0]))(\\.(\\d)*))");

    @Override
    protected Object convert(String value) {
        if(StringUtils.isBlank(value)){
            return BigDecimal.ZERO;
        }
        Matcher matcher = DECIMAL.matcher(value);
        if(matcher.find()) {
            return new BigDecimal(matcher.group());
        }
        return BigDecimal.ZERO;
    }
}
